/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Location;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Sony
 */
@ManagedBean(name = "locations")
@RequestScoped
public class LocationController {

    private Location newLocation;
    LocationHelper helper;
    private DataModel listLocation;

    public LocationController() {
        helper = new LocationHelper();
        newLocation = new Location();
        listLocation = new ListDataModel(this.helper.getAllLocation());
    }
    
    public void refreshTable(){
         RequestContext requestContext = RequestContext.getCurrentInstance();
        requestContext.update("table1");
    }

    public String addLocation() {
        helper.updateLocation(newLocation);
        listLocation = new ListDataModel(this.helper.getAllLocation());
        RequestContext requestContext = RequestContext.getCurrentInstance();
        requestContext.update("table1");
        return null;
    }

    /**
     * @return the newLocation
     */
    public Location getNewLocation() {
        return newLocation;
    }

    /**
     * @param newLocation the newLocation to set
     */
    public void setNewLocation(Location newLocation) {
        this.newLocation = newLocation;
    }

    /**
     * @return the listLocation
     */
    public DataModel listLocation() {
        listLocation = new ListDataModel(this.helper.getAllLocation());
        return listLocation;
    }
 public String listLocation2() {
        listLocation = new ListDataModel(this.helper.getAllLocation());
        return null;
    }
    public void deleteLocation(Location c){
        this.helper.deleteLocation(c);
        listLocation = new ListDataModel(this.helper.getAllLocation());
    }

    
}
